<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h1>{{msg}}</h1>
      <tree-menus :list="list"></tree-menus>
    </div>

    <div class="box"></div>

    <script src="./tree.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
      // console.log('it:', it.getData())

      let list = it.getData()

      Vue.component('tree-menus', {
        props: {
          list: Array,
        },
        template: `
            <ul style="padding-left: 20px;" >
              <template v-for="(item,index) in list">
                <li :key="index" v-if="item.show" @click.stop="handToggleShow(item)">
                  <p >{{item.name}}</p>
                  <tree-menus v-if="item.children"  :list="item.children"></tree-menus>
                </li>
              </template>
            </ul>
          `,
        methods: {
          handToggleShow(item) {
            console.log(item.name, 'name')
            if (item.children) {
              item.children.forEach((item) => {
                item.show = !item.show
              })
            }
          },
        },
      })

      new Vue({
        el: '#app',
        data: {
          msg: 'vue',
          list: [],
        },
        created() {
          this.list = it.getData()
          this.recursionData(this.list)
          this.list.forEach((item) => {
            item.show = true
          })
        },
        methods: {
          recursionData(list) {
            list.forEach((item) => {
              this.$set(item, 'show', false)
              if (item.children) {
                this.recursionData(item.children)
              }
            })
          },
        },
      })
    </script>
  </body>
</html>
